using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Zune.Forms
{
    /// <summary>
    /// A MenuItem.
    /// </summary>
    public class MenuItem
    {
        #region Internal Events/Delegates
        internal event EventHandler Clicked;
        internal delegate void OnDisplayContextMenu(object sender, GameTime gameTime);
        internal event OnDisplayContextMenu DisplayContextMenu;
        #endregion

        #region Private Enums
        private enum MenuItemTypes
        {
            Text,
            Seperator
        }
        #endregion

        #region Private/Internal Members
        private ContextMenu _menu;
        private MenuItemTypes _menuItemType = MenuItemTypes.Text;
        private Texture2D _seperator;
        private string _text = "MenuItem";
        internal Vector2 _location;
        private bool _isPressed = false;
        private object _value;
        private bool _enabled = true;
        #endregion

        #region Constructors
        /// <summary>
        /// A MenuItem.
        /// </summary>
        /// <param name="menu">The ContextMenu the menu belongs to.</param>
        public MenuItem(ContextMenu menu)
        {
            _menu = menu;
            _menu.MenuItems.Add(this);
        }
        #endregion

        #region Internal Methods
        internal void LoadContent()
        {
            _seperator = Drawing.CreateLine(_menu.GraphicsDevice, (Int32)_menu.Left + 3, 0, (Int32)_menu.Right - 3, 1, _menu.ForeColor);
        }

        internal void Draw(GameTime gameTime, Texture2D background, Texture2D pressedBackground)
        {
            switch (_menuItemType)
            {
                case MenuItemTypes.Seperator:
                    //_menu.SpriteBatch.Draw(_seperator, new Vector2(_menu.Left + _menu._menItemSize.X, _menu.Top + (_menu._menItemSize.Y * index)), Color.White);
                    break;
                case MenuItemTypes.Text:

                    if (_isPressed)
                    {
                        _menu.SpriteBatch.Draw(pressedBackground, _location, Color.White);
                        _menu.SpriteBatch.DrawString(_menu._font, _text, new Vector2(_location.X, _location.Y), _menu._pressedForeColor);
                    }
                    else if (!_enabled)
                    {
                        _menu.SpriteBatch.Draw(background, _location, Color.White);
                        _menu.SpriteBatch.DrawString(_menu._font, _text, new Vector2(_location.X, _location.Y), _menu._disabledForeColor);
                    }
                    else
                    {
                        _menu.SpriteBatch.Draw(background, _location, Color.White);
                        _menu.SpriteBatch.DrawString(_menu._font, _text, new Vector2(_location.X, _location.Y), _menu._foreColor);
                    }    
                    break;
            }
        }

        internal void Update(GameTime gameTime, TouchLocation touch)
        {
            if (Utilities.IsControlTouched(touch.Position, _location, _menu._menItemSize))
            {
                switch (touch.State)
                {
                    case TouchLocationState.Pressed:
                    case TouchLocationState.Moved:
                        if (_enabled)
                            _isPressed = true;
                        break;
                    case TouchLocationState.Released:
                        if (_enabled)
                        {
                            if (Clicked != null)
                                Clicked(this, EventArgs.Empty);
                        }
                        _isPressed = false;
                        break;
                }
            }
        }
        #endregion

        #region Public Properties
        /// <summary>
        /// Gets or sets the text.
        /// </summary>
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }

        /// <summary>
        /// Gets or sets the object value.
        /// </summary>
        public object Value
        {
            get { return _value; }
            set { _value = value; }
        }

        /// <summary>
        /// Gets or sets whether the MenuItem is enabled.
        /// </summary>
        public bool Enabled
        {
            get { return _enabled; }
            set { _enabled = value; }
        }
        #endregion
    }
}